<?php
/**
 * spsNewsletter Default Controller
 * 
 * @author Satyan Shah <joomlaDesign@modernbridge.com>
 * @link http://www.modernbridge.com
 * @license GNU/GPL
 */

jimport('joomla.application.component.controller');

// extend the JController class for newsletter specific tasks
class spsNewsletterController extends JController
{
	function display()
	{
		// switch on the requested task
		switch(JRequest::getVar('task'))
		{
  
			case 'addSubscriber':
			
				// read in a few variables from the post data 
				$name = JRequest::getVar( 'name','', 'post', 'string');
				$email = JRequest::getVar( 'email','', 'post', 'string');
		
				// make sure both a name and email were supplied
				if(!$email)
				{
					JRequest::setVar('operationStatus','One or more required fields is missing');
					break; // <- Exit Point. Task Failed.
				}
				
				// Get the model
				$model = $this->getModel();
				// Invoke the add subscriber method
				$addSubscriberStatus=$model->addSubscriber($name,$email); 
				// Check the return status and return the appropriate status string to the user
				if($addSubscriberStatus==-1)
				  JRequest::setVar( 'operationStatus', 'Your Email Address is already in our database' );
				if($addSubscriberStatus==1)
				  JRequest::setVar( 'operationStatus', 'Email Address Added Successfully' );
				if($addSubscriberStatus==0)
				  JRequest::setVar('operationStatus', 'Your email address could not be stored');
				  
			break;
      
			// remove a subscriber by id
			case 'removeSubscriber':
				// set the view to remove instead of default
				JRequest::setVar( 'view', 'remove' );
				// read in the subscriber id from the get data (URL)
				$removeID = JRequest::getVar( 'subscriberID','', 'get', 'string');
				// get the model
				$model = $this->getModel();
				// attempt to remove the subscriber, return status to user via operationStatus variable
				if($model->removeSubscriber($removeID))
					JRequest::setVar( 'operationStatus', 'Email Address Deleted Successfully' );
				else
					JRequest::setVar('operationStatus', 'Your email address could not be deleted!');
			break;
			
			// remove a subscriber by email
			case 'removeSubscriberByEmail':
				// set the view to remove instead of default
				JRequest::setVar( 'view', 'remove' );
				// read in the subscribers email address
				$email = JRequest::getVar( 'email','', 'post', 'string');
				// get the model
				$model = $this->getModel();
				// attempt to remove the subscriber, return status to user via operationStatus variable
				if($model->removeSubscriberByEmail($email))
					JRequest::setVar( 'operationStatus', 'Email Address Deleted Successfully' );
				else
					JRequest::setVar('operationStatus', 'Your email address could not be deleted!');
			break;
    
			// no operations required if the task isn't add or remove
			default:
			break;
      
		}
    
  
		parent::display();
	
	}

}
?>
